using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Web;

namespace OpenWaves
{
    [Serializable]
    [TypeConverter(typeof(UrlTypeConverter))]
    public partial class Url : ISerializable
    {
        protected Url(SerializationInfo info, StreamingContext context)
        {
            this.uri = new Uri(info.GetString("uri"));
        }

        #region ISerializable Members

        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("uri", this.uri.ToString());
        }

        #endregion

        public static string Encode(string value)
        {
            Contract.Requires<ArgumentNullException>(value != null);
            Contract.Ensures(Contract.Result<string>() != null);

            return HttpUtility.UrlEncode(value).Replace("+", "%20");
        }

        public static string Decode(string value)
        {
            Contract.Requires<ArgumentNullException>(value != null);
            Contract.Ensures(Contract.Result<string>() != null);

            return HttpUtility.UrlDecode(value);
        }
    }
}